<?php

namespace App\Http\Controllers;

use App\TypeModel;
use Illuminate\Http\Request;
use mysql_xdevapi\Result;
use phpDocumentor\Reflection\Location;

class TypeController extends Controller
{
    public function addtype()
    {
        return view("admin.addtype");
    }


    public function typeinsert(Request $request)
    {
        $type = $request->input();
        $id = $request->input("id");
        if ($id == 0) {
            TypeModel::create($type);
            $result = ["title" => "数据添加成功", "err" => 0];
        } else {
            $ok = TypeModel::where("id", $id)->first();
            $ok->update($type);
            $result = ["title" => "数据修改成功", "err" => 0];
        }
        $result = json_encode($result);
        return $result;
    }


    public function types()
    {
        $type = TypeModel::get();
//        dd($types);
        return view("admin.types", compact("type"));
    }


    public function deletetype(Request $request)
    {
        $id = $request->input("id");
        TypeModel::where("id", $id)->delete();
        $result = ["title" => "删除成功", "err" => 0];
        $result = json_encode($result);
        return $result;
    }

    public function updatetype(Request $request)
    {
        $id = $request->input("id");
        $ok = TypeModel::where("id", $id)->find($id);
        return view("admin.addtype", compact("ok"));
    }
}
